package com.fisher.checker;

import android.content.Context;
import android.content.SharedPreferences;

public class AppPreferences {
    private static final String PREF_NAME = "MyAppPreferences";
    private static final String KEY_FIRST_RUN = "firstRun";
    public static final String KEY_ENCRYPTED_UUID = "encrypted_uuid";

    public static final String KEY_SHARED_PUBLIC_KEY = "shared_public_key";

    public static final String KEY_ACTIVATE_ENCRYPTED_UUID="activate_encrypted_uuid";
    public static final String KEY_ACTIVATE_LICENSE_FILE ="activate_license_file";
    public static final String KEY_ACTIVATE_LICENSE_FILE_SCANNER ="activate_license_file_scanner";

    public static boolean isFirstRun(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        boolean firstRun = preferences.getBoolean(KEY_FIRST_RUN, true);
        if (firstRun) {
            // 首次启动，设置标志为 false
            preferences.edit().putBoolean(KEY_FIRST_RUN, false).apply();
        }
        return firstRun;
    }

    public static void reset(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        boolean firstRun = preferences.getBoolean(KEY_FIRST_RUN, true);
        if (!firstRun) {
            // 首次启动，设置标志为 false
            preferences.edit().putBoolean(KEY_FIRST_RUN, true).apply();
        }

    }

    public static void setEncryptedUUID(Context context, String encryptedUUID) {
        SharedPreferences preferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        preferences.edit().putString(KEY_ENCRYPTED_UUID, encryptedUUID).apply();
    }

    public static String getEncryptedUUID(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        return preferences.getString(KEY_ENCRYPTED_UUID,"");
    }

    public static void saveSharedPublicKey(Context context, String publicKey) {
        SharedPreferences preferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        preferences.edit().putString(KEY_SHARED_PUBLIC_KEY, publicKey).apply();
    }

    public static void save2Preferences(Context context, String key, String value){
        SharedPreferences preferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        preferences.edit().putString(key, value).apply();
    }

    public static String getFromPreferences(Context context, String key){
        SharedPreferences preferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        return preferences.getString(key, null);
    }

    public static String getSharedPublicKey(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        return preferences.getString(KEY_SHARED_PUBLIC_KEY, null);
    }
}
